# LoginDialog.py
# Copyright (C) otacon 2010 <orfeo18@gmail.com>
# 
# AdSense-Monitor is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# 
# AdSense-Monitor is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License along
# with this program.  If not, see <http://www.gnu.org/licenses/>.

import gobject
import os
import sys

from core import Path

try:
	import pygtk
	#pygtk.require("2.17.0")
except:
	print "pygtk not found"
	sys.exit(1)
try:
	import gtk
except:
	print "gtk or glade not found"
	sys.exit(1)

class LoginDialog(gobject.GObject):
    
    def __init__( self , configuration ):
        ''' This class implements the settings dialog '''
        gobject.GObject.__init__(self)
        builder = os.path.join(Path.WORKING_PATH,'gui',"Login.ui")
        self._ui = gtk.Builder()
        self._ui.add_from_file( builder )

        self.widgets = {}
        self.values = {}
        self.generate_config_bindings()
        self.set_configuration(configuration)
        self._ui.connect_signals(self)
    
    def set_configuration(self, configuration ):
        self.configuration = configuration
        username = self.configuration.get_preference('username')
        password = self.configuration.get_preference('password')
        save_password = self.configuration.get_preference('save_password')
        self.widgets['username'].set_text(username)
        self.widgets['password'].set_text(password)
        self.widgets['save_password'].set_active(save_password)
    
    def generate_config_bindings(self):
        self.widgets['username'] = self._ui.get_object('username')
        self.widgets['password'] = self._ui.get_object('password')
        self.widgets['save_password'] = self._ui.get_object('save_password')

    def open_dialog(self):
        self._ui.get_object("login_dialog").show()

    def set_message(self,message):
        self._ui.get_object("message").set_text(message)

    def on_button_clicked(self,button):
        if button == self._ui.get_object("ok"):
            incomplete_data=True
            if self.widgets['username'].get_text() and self.widgets['password'].get_text():
                self.values['username'] = self.widgets['username'].get_text()
                self.values['password'] = self.widgets['password'].get_text()
                incomplete_data=False
            if self.widgets['save_password'].get_active():
                self.values['save_password'] = self.widgets['save_password'].get_active()
            if not incomplete_data:
                self._ui.get_object("login_dialog").hide()
                self.set_message('')
                self.configuration.set_preferences(self.values)
            else:
                self.set_message('You have to complete username and password fields')
        else:
            self.set_message('')
            self._ui.get_object("login_dialog").hide()
